package com.t.bricks.bussiness.service.synchtable.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.t.bricks.bussiness.db.dao.synchtable.SynchTableDao;
import com.t.bricks.bussiness.db.entity.synchtable.SynchTable;
import com.t.bricks.bussiness.service.synchtable.SynchTableService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.UUIDUtil;
import com.t.bricks.utils.function.date.TimeUtil;
import com.t.bricks.utils.function.reflect.ReflectUtil;

/**
 * 同步表信息'SynchTable表'基本业务操作类接口实现
 * @author HuangXinBian
 * @DateTime 2021年08月25日 15:16:19
 */
@Service
public class SynchTableServiceImpl implements SynchTableService {

	@Autowired
	private SynchTableDao synchTableDao;

	/**
	 * 数据保存
	 * @Param synchTable
	 * @return MsgEmity
	 */
	@Override
	@Transactional//启用事务
	public MsgEmity save(SynchTable synchTable) {
		if (null == synchTable) {
			return MsgEmity.err(8001, "参数为空");
		}
		
		ReflectUtil.trimAttribute(synchTable);//清理属性的前后空格
		
		//--无论有没有数据,先删除再添加--//
		synchTableDao.del(synchTable.getsTable(), synchTable.getsDataId(), synchTable.getsOwner());
		synchTable.setuId(UUIDUtil.get());
		MsgEmity me = synchTableDao.add(synchTable);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.setMsg("保存成功");
	}

	/**
	 * 删除
	 * @Param sTable
	 * @Param sDataId
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity del(String sTable, String sDataId, String sOwner) {
		if (null == sTable || "".equals(sTable.trim())) {
			return MsgEmity.err(8001, "同步的表名参数为空");
		}
		
		if (null == sDataId || "".equals(sDataId.trim())) {
			return MsgEmity.err(8002, "数据编号参数为空");
		}
		
		if (null == sOwner || "".equals(sOwner.trim())) {
			return MsgEmity.err(8003, "所有者参数为空");
		}
		
		return synchTableDao.del(sTable, sDataId, sOwner);
	}
	
	/**
	 * 查询当前表最后的时间
	 * @Param sTable
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findMaxDate(String sTable, String sOwner) {
		if (null == sTable || "".equals(sTable.trim())) {
			return MsgEmity.err(8001, "'同步的表名'参数为空");
		}
		
		if (null == sOwner || "".equals(sOwner.trim())) {
			return MsgEmity.err(8002, "所有者参数为空");
		}
		
		return synchTableDao.findMaxDate(sTable, sOwner);
	}
	
	/**
	 * 查询当前表最后的时间(格式:yyyy-MM-dd HH:mm:ss)
	 * @Param sTable
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findMaxDateStr(String sTable, String sOwner) {
		if (null == sTable || "".equals(sTable.trim())) {
			return MsgEmity.err(8001, "'同步的表名'参数为空");
		}
		
		if (null == sOwner || "".equals(sOwner.trim())) {
			return MsgEmity.err(8002, "所有者参数为空");
		}
		
		return synchTableDao.findMaxDateStr(sTable.trim(), sOwner.trim());
	}
	
	/**
	 * 查询当前表最后的时间
	 * @Param sTable
	 * @Param sOwner
	 * @Param dDateSt
	 * @Param dDateEd
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByDate(String sTable, String sOwner, String dDateSt, String dDateEd) {
		if (null == dDateSt || "".equals(dDateSt.trim())) {
			return MsgEmity.err(8001, "开始时间参数为空");
		}
		
		if (null == sOwner || "".equals(sOwner.trim())) {
			return MsgEmity.err(8002, "所有者参数为空");
		}
		
		Date st = TimeUtil.toDate(dDateSt);
		if (null == st) {
			return MsgEmity.err(8003, "开始时间参数不是正确的时间格式");
		}
		
		Date ed = null;
		if (null != dDateEd && !"".equals(dDateEd.trim())) {
			ed = TimeUtil.toDate(dDateEd);
		}
		
		return synchTableDao.findByDate(sTable, sOwner, st, ed);
	}
	
	/**
	 * 清理数据
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity clear(String sOwner) {
		if (null == sOwner || "".equals(sOwner.trim())) {
			return MsgEmity.err(8001, "所有者参数为空");
		}

		return synchTableDao.clear(sOwner);
	}

	/**
	 * 查询各用户最后同步时间
	 * @return
	 */
	@Override
	public MsgEmity findLastDate() {
		return synchTableDao.findLastDate();
	}
	
}
